#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using vec = vector < int >;
using ll = long long;
using pii = pair < ll , int >;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 5e5 + 10;

ll dis[N];
int a, b, c, d, e, n, l[N], r[N];
vec G[N];
bool vis[N];

ll getdis(int s, int t) {
	memset(dis, 0x3f, sizeof dis); dis[s] = 0; memset(vis, 0, sizeof vis);
	priority_queue < pii, vector < pii >, greater < pii >> q; q.push({ dis[s], s });
	while(q.size()) {
		int x = q.top().se; q.pop(); if(vis[x]) continue; vis[x] = true;
		for(auto y : G[x]) {
			int d = abs(y - x);
			if(dis[x] + d < dis[y]) dis[y] = dis[x] + d, q.push({ dis[y], y });
		}
	}
	return dis[t];
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    a = in, b = in, c = in, d = in, e = in; b += a; c += b; d += c; e += d;
	n = in; rep(i, 1, n) l[i] = in, r[i] = in, G[r[i] + 1].pb(l[i]), G[l[i]].pb(r[i] + 1);
	ll ans = 0x3f3f3f3f3f3f3f3f, d1, d2;
	d1 = getdis(a + 1, b + 1), d2 = getdis(c + 1, d + 1);
	ans = min(ans, d1 + d2);
	d1 = getdis(a + 1, d + 1), d2 = getdis(b + 1, c + 1);
	ans = min(ans, d1 + d2);
	d1 = getdis(a + 1, c + 1), d2 = getdis(b + 1, d + 1);
	ans = min(ans, d1 + d2);
	cout << (ans < 0x3f3f3f3f3f3f3f3f ? ans : - 1) << endl; return 0;
}
